package org.jeecg.modules.wangyue.controller.sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.entity.CapitalFlow;
import org.jeecg.modules.wangyue.service.ICapitalFlowService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 资金流水管理
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags="资金流水")
@RestController
@RequestMapping("/wangyue/sys/capitalFlow")
public class CapitalFlowManageController extends JeecgController<CapitalFlow, ICapitalFlowService> {
	@Autowired
	private ICapitalFlowService capitalFlowService;
	@Autowired
	private ExportUtil exportUtil;


	/**
	 * 资金流水管理-分页列表查询
	 *
	 * @param keywords		关键字
	 * @param userType		用户类型
	 * @param changeType	变动类型(0-充值，1-消费，2-退款，3-提现，4-订单收入，11-平台分佣转入，12-平台业绩转入，15-积分兑换)
	 * @param beginTime		开始时间
	 * @param endTime		结束时间
	 * @param pageNo		第几页
	 * @param pageSize		每页大小
	 * @return
	 */
	@AutoLog(value = "资金流水管理-分页列表查询")
	@ApiOperation(value="资金流水管理-分页列表查询", notes="资金流水管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(@RequestParam(name="keywords", required=false) String keywords,
								   @RequestParam(name="userType", required=false) String userType,
								   @RequestParam(name="changeType", required=false) String changeType,
								   @RequestParam(name="beginTime", required = false) String beginTime,
								   @RequestParam(name="endTime", required = false) String endTime,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<CapitalFlow> queryWrapper = new QueryWrapper<>();
		if(oConvertUtils.isNotEmpty(keywords)){
			queryWrapper.like("account", keywords);
		}
		if(oConvertUtils.isNotEmpty(userType)){
			queryWrapper.eq("user_type", userType);
		}
		if(oConvertUtils.isNotEmpty(changeType)){
			queryWrapper.eq("change_type", changeType);
		}
		if(oConvertUtils.isNotEmpty(beginTime) && oConvertUtils.isNotEmpty(endTime)){
			queryWrapper.apply("create_time >= '" + beginTime + "' and create_time <= '" + endTime + "'");
		}
		queryWrapper.eq("del_flag", DelFlagConstant.normal);
		queryWrapper.orderByDesc("create_time");
		Page<CapitalFlow> page = new Page<CapitalFlow>(pageNo, pageSize);
		IPage<CapitalFlow> pageList = capitalFlowService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 导出excel
	  *
	  * @param capitalFlow
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(CapitalFlow capitalFlow) {
		 QueryWrapper<CapitalFlow> queryWrapper = new QueryWrapper<>();
		 if(oConvertUtils.isNotEmpty(capitalFlow.getIds())){
			 queryWrapper.in("id", Arrays.asList(capitalFlow.getIds().split(",")));
		 }
		 if(oConvertUtils.isNotEmpty(capitalFlow.getKeywords())){
			 queryWrapper.like("account", capitalFlow.getKeywords());
		 }
		 if(oConvertUtils.isNotEmpty(capitalFlow.getUserType())){
			 queryWrapper.eq("user_type", capitalFlow.getUserType());
		 }
		 if(oConvertUtils.isNotEmpty(capitalFlow.getChangeType())){
			 queryWrapper.eq("change_type", capitalFlow.getChangeType());
		 }
		 if(oConvertUtils.isNotEmpty(capitalFlow.getBeginTime()) && oConvertUtils.isNotEmpty(capitalFlow.getEndTime())){
			 queryWrapper.apply("create_time >= '" + capitalFlow.getBeginTime() + "' and create_time <= '" + capitalFlow.getEndTime() + "'");
		 }
		 queryWrapper.eq("del_flag", DelFlagConstant.normal);
		 queryWrapper.orderByDesc("create_time");
		 List<CapitalFlow> capitalFlowList = capitalFlowService.list(queryWrapper);

		 return exportUtil.exportXls(capitalFlowList, CapitalFlow.class, "资金流水数据");
	 }

	


}
